package com.group.controller;

import com.group.service.MerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.entity.Good;
import utils.BaseController;

import javax.servlet.http.HttpServletResponse;

@Api(tags = "商家后台")
@Controller
@RequestMapping(value = "/merchant" , produces = "text/html;charset=UTF-8")
public class MerchantController extends BaseController {

    @Autowired
    private MerchantService merchantService;

    @ApiOperation("商品列表")
    @RequestMapping(value = "/selectListAllController.do", method = RequestMethod.GET)
    @ResponseBody
    public String selectListAllController(HttpServletResponse response) {
        return writerSuccessResult(merchantService.selectListAll(), response);
    }

    @ApiOperation("商品下架")
    @RequestMapping(value = "/goodsUpController.do", method = RequestMethod.POST)
    @ResponseBody
    public String goodsUpController(@RequestBody Good good, HttpServletResponse response) {
        return writerSuccessResult(merchantService.updateByIdType(good), response);
    }

    @ApiOperation("删除商品")
    @RequestMapping(value = "/deleteByIdController.do", method = RequestMethod.POST)
    @ResponseBody
    public String deleteByIdController(@RequestBody Long goodId, HttpServletResponse response) {
        int i = merchantService.deleteById(goodId);
        System.out.println(i);
        return writerSuccessResult(merchantService.deleteById(goodId), response);
    }

    @ApiOperation("修改库存")
    @RequestMapping(value = "/updateByIdCountController.do", method = RequestMethod.POST)
    @ResponseBody
    public String updateByIdCountController(@RequestBody Good good, HttpServletResponse response) {
        Long goodCount = good.getGoodId();
        System.out.println("updateByIdCountController"+goodCount);
        return writerSuccessResult(merchantService.updateByIdCount(good), response);
    }

    @ApiOperation("模糊查询")
    @RequestMapping(value = "/SearchController.do", method = RequestMethod.POST)
    @ResponseBody
    public String SearchController(@RequestBody Good good, HttpServletResponse response) {
        return writerSuccessResult(merchantService.Search(good), response);
    }

    @ApiOperation("分页")
    @RequestMapping(value = "/pagingController.do", method = RequestMethod.POST)
    @ResponseBody
    public String pagingController(@RequestBody Good good, HttpServletResponse response) {
        good.setNewPages((good.getNewPages()-1)*good.getPages());
        return writerSuccessResult(merchantService.paging(good), response);
    }

}
